/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.CarDetailBusiness;
import business.CarModelBusiness;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.CarDetails;
import model.CarModel;

/**
 *
 * @author Nhang Con
 */
@ManagedBean
@RequestScoped
public class CarDetailBean {

    private int idModel;
    private CarDetails car;
    private List<CarDetails> lcar;
    private List<CarModel> lmodel;
    private int pageID;
    private List<Integer> pageList;

    public int getPageID() {
        return pageID;
    }

    public void setPageID(int pageID) {
        this.pageID = pageID;
    }

    public List<Integer> getPageList() {
        return pageList;
    }

    public void setPageList(List<Integer> pageList) {
        this.pageList = pageList;
    }

    public List<CarModel> getLmodel() {
        return lmodel;
    }

    public void setLmodel(List<CarModel> lmodel) {
        this.lmodel = lmodel;
    }

    public CarDetails getCar() {
        return car;
    }

    public void setCar(CarDetails car) {
        this.car = car;
    }

    public int getIdModel() {
        return idModel;
    }

    public void setIdModel(int idModel) {
        this.idModel = idModel;
    }

    public List<CarDetails> getLcar() {
        return lcar;
    }

    public void setLcar(List<CarDetails> lcar) {
        this.lcar = lcar;
    }
    private CarDetailBusiness cb = new CarDetailBusiness();
    private CarModelBusiness cmb = new CarModelBusiness();

    /** Creates a new instance of CarDetailBean */
    public CarDetailBean() {
         
       
        int pageIndex = cb.getRowbyID();
        pageList = new ArrayList<Integer>();
        if (pageIndex % 3 == 0) {
            for (int i = 1; i <= pageIndex / 3; i++) {
                pageList.add(i);
            }
        } else {
            for (int i = 1; i <= pageIndex / 3 + 1; i++) {
                pageList.add(i);
            }
        }
         car = new CarDetails();
        if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID1") != null) {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID1").toString());
        } else {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("PageID2").toString());
        }

        lcar = cb.listCarDetailByPage(pageID);
        lmodel = cmb.getListModel();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("PageID2", pageID);

    }

    public String actionCreateDetail() {

        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (session.getAttribute("sessionFile2") != null) {
            String path = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("sessionFile2").toString();
            String getfile = "images/upload/" + path;
            car.setImages(getfile);
            car.setModelID(idModel);
            if (cb.createCarDetail(car)) {

                lmodel = cmb.getListModel();
                return "managercardetail.xhtml?pageID1=1";

            }
            return "error.xhtml";
        }

        return "error.xhtml";
    }

    public String updateCarDetail(CarDetails car) {

        if (cb.updateCarDetail(car)) {
            return "managercardetail.xhtml?pageID1=1";
        }
        return "error.xhtml";
    }
   

    
}
